package heman.server.controllers;




import heman.server.database.DatabaseController;
import heman.server.entities.Player;
import heman.server.entities.PlayerManager;
import ks.framework.common.Message;
import ks.framework.communicator.Communicator;

import ks.server.interfaces.IProcessServerMessage;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * 
 *  Controller to handle the change of player's profile. 
 *  
 *  <getProfile player="1932" /> 
 *    @author Wenlong Liu
 *  */

public class ProfileController implements IProcessServerMessage {

	public boolean process(Communicator com, Message m) {
		String originator=m.getOriginator();
		
       
        Node root = m.contents();
        NamedNodeMap map=root.getAttributes();

        Node playerNode=map.
        		getNamedItem("player");

        
        if (playerNode ==null) return false;
        String playerID=playerNode.getNodeValue();
        
        
        Player player=DatabaseController.getPlayer(playerID);
        if (player==null) return false;
        
        StringBuilder sb = new StringBuilder(Message.responseHeader(true,m.id));
        //sb.append("")
        sb.append("<playerResponse>");
        
        sb.append(player.toXMLPlayer());
        sb.append("</playerResponse></response>");
        
        Document d = Message.construct(sb.toString());
        
        Message r = new Message (d);
        r.setOriginator(originator);
        r.setRecipient(originator);
        
        return com.distribute(r);
        	
      
        
	}
}